package com.yanxu.lock.lock;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025/3/16-2025
 * @Description: 乐观锁
 */
public class Lock_Optimistic {

    private static AtomicInteger atomicInteger = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(100000);
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 100000; i++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        atomicInteger.incrementAndGet();
                    }finally {
                        countDownLatch.countDown();
                    }
                }
            });
        }

        countDownLatch.await();
        System.out.println(" 最终结果为："+ atomicInteger);

    }
}